﻿using Microsoft.AspNetCore.Mvc;
using System.Drawing;
using System.Text;
using Wechat.Webapi;
using Wechat.Webapi.Modal;
using Wechat.Webapi.Modal;
using ZXing;
using static System.Net.Mime.MediaTypeNames;
using System.Net;

namespace WeixinTest.Controllers
{
    [ApiController]
    [Route("[controller]/[action]")]
    public class WeixinCOntroller : ControllerBase
    {
        [HttpGet(Name = "Login")]
        public FileContentResult Login()
        {
            WeixinClient wexclient = new WeixinClient();
            wexclient.Start();
            while (true)
            {
                string uuid = wexclient.client.uuid;
                if (string.IsNullOrWhiteSpace(uuid))
                {
                    continue;
                }
                System.Drawing.Image image = wexclient.client.GetERcode(uuid);
                HT.allClients[wexclient.client.uuid] = wexclient;
                using (var memoryStream = new MemoryStream())
                {
                    // 将Image保存到MemoryStream中
                    image.Save(memoryStream, image.RawFormat);
                    // 返回MemoryStream中的字节数组
                    return File(memoryStream.ToArray(), "image/png");
                }
                wexclient.client.SendMsg("filehelper", "登录成功");
            }

        }
        [HttpGet(Name = "getuuidlist")]
        public ActionResult getuuidlist()
        {
            return Ok(HT.allClients.Keys);
        }
        [HttpGet(Name = "logout")]
        public ActionResult logout(string uuid)
        {
            HT.allClients[uuid].client.Close();
            HT.allClients[uuid].client.Logout();
            HT.allClients.Remove(uuid);
            return Ok("成功");
        }

    }
    public static class HT
    {
        public static Dictionary<string, WeixinClient> allClients = new();

    }
}
